package io.linfeng.service.im.server.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.linfeng.api.app.guest.response.GuestResponse;
import io.linfeng.api.app.im.reponse.MessageRecordResponse;
import io.linfeng.api.app.im.request.MessageRecordRequest;
import io.linfeng.common.utils.PojoInfoUtil;
import io.linfeng.common.utils.RedisUtil;
import io.linfeng.common.utils.StringUtil;
import io.linfeng.service.im.entity.SocketMsg;
import io.linfeng.service.im.mapper.MessageRecordMapper;
import io.linfeng.service.im.entity.MessageRecordEntity;
import io.linfeng.service.im.server.MessageRecordService;
import io.linfeng.service.user.entity.UserEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author RJF
 * @since 2024-05-11
 */
@Service("messageRecordService")
public class MessageRecordServiceImpl extends ServiceImpl<MessageRecordMapper, MessageRecordEntity> implements MessageRecordService {

    @Override
    public void save(UserEntity userEntity, SocketMsg socketMsg) {
        userEntity = new UserEntity();
        userEntity.setUid(100);
        MessageRecordEntity entity = new MessageRecordEntity();
        entity.setCreateUsername(userEntity.getUserName());
        entity.setGroupName("");
        entity.setMsg(socketMsg.getMsg());
        entity.setReadStatus(0);
        entity.setLastUpdateUsername(userEntity.getUserName());
        entity.setType(socketMsg.getType());
        entity.setReceiveUserId(userEntity.getUid());
        entity.setSendOutUserId(userEntity.getUid());
        this.save(entity);
    }

    @Override
    public List<MessageRecordResponse> getMessage(MessageRecordRequest request) {
        LambdaQueryWrapper<MessageRecordEntity> wrapper = new LambdaQueryWrapper<MessageRecordEntity>()
                .eq(!StringUtil.isEmpty(request.getSendUserId()), MessageRecordEntity::getSendOutUserId, request.getSendUserId())
                .eq(!StringUtil.isEmpty(request.getReceiveUserId()), MessageRecordEntity::getReceiveUserId, request.getReceiveUserId())
                .or()
                .eq(!StringUtil.isEmpty(request.getSendUserId()), MessageRecordEntity::getReceiveUserId, request.getSendUserId())
                .eq(!StringUtil.isEmpty(request.getReceiveUserId()), MessageRecordEntity::getSendOutUserId, request.getReceiveUserId())
                .orderByAsc(MessageRecordEntity::getSendTime);
        List<MessageRecordEntity> list = this.list(wrapper);
        List<MessageRecordResponse> response = PojoInfoUtil.listInfo2DTO(list, MessageRecordResponse.class);
        return response;
    }
}
